<?php

namespace App\Jobs;

use App\Business\BLL\Wechat\Official;
use App\Models\Reservation;
use App\Models\WechatUser;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendReservationReject implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $game;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Reservation $reservation)
    {
        $this->game = $reservation;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if ($this->game->type == 1) {
            $playA = WechatUser::find($this->game->play_a);
            $playB = WechatUser::find($this->game->play_b);
        } else {
            $sectMemberA = SectMember::where([['title', 10], ['sect_id', $this->game->play_a]])->first();
            $playA = WechatUser::find($sectMemberA->user_id);
            $sectB = Sect::find($this->game->play_b);
        }
        if (empty($playA->wechat_openid)) {
            return;
        }
        $touser = $playA->wechat_openid;
        if ($this->game->type == 1) {
            $first = '对方拒绝了你的挑战';
            $keyword1 = $playB->name;
        } else {
            $first = '对方拒绝了你的门派战';
            $keyword1 = $sectB->name;
        }

        //发送
        $official = Official::make();
        $official->template_message->send([
            'touser' => $touser,
            'template_id' => 'bLFDuOEzWSEZagxWfszVeW7XMqIAm47AlgtgzHNw7jM',
            'miniprogram' => [
                'appid' => config('wechat.mini_program.default.app_id'),
                'pagepath' => 'pages/reservation/reservationInfo?id=' . $this->game->id,
            ],
            'data' => [
                'first' => $first,
                'keyword1' => $keyword1,
                'keyword2' => $this->game->game_datetime,
                'keyword3' => $this->game->address,
                'remark' => '您可以重新预约其他人对战',
            ],
        ]);

    }
}
